#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define LEN 20
#define MAX 12

struct seat {
    int num;
    int is_assign;
    char f_name[LEN];
    char l_name[LEN];
};

void read_file(struct seat *);
void write_file(struct seat *);
void init(struct seat *);
char get_choice(void);
void show_number(struct seat *);    //空位置的个数
void show_empty(struct seat *);
void show_list(struct seat *);
void assign_seat(struct seat *);
void delete_seat(struct seat *);
char * s_gets(char * st, int n);

int main(void)
{
    char ch;
    struct seat customer[MAX];

    init(customer);
    read_file(customer);

    // 用户输入选项
    while ((ch = get_choice()) != 'f')
    {
        switch (ch) {
            case 'a':
                show_number(customer);
                break;
            case 'b':
                show_empty(customer);
                break;
            case 'c':
                show_list(customer);
                break;
            case 'd':
                assign_seat(customer);
                break;
            case 'e':
                delete_seat(customer);
                break;
            default:
                printf("Program error.\n");
                break;
        }
    }
    // 将数据输入至文件中
    write_file(customer);
}

void init(struct seat * ps)
{
    int i;

    for (i = 0; i < MAX; i++)
    {
        ps[i].num = i+1;
        ps[i].is_assign = 0;
        strcpy(ps[i].f_name, "");
        strcpy(ps[i].l_name, "");
    }
}

void read_file(struct seat * ps)
{
    FILE * fp;

    if ((fp = fopen("seat.txt", "a+b")) == NULL)
    {
        fprintf(stderr, "Can't open seat.txt.\n");
        exit(EXIT_FAILURE);
    }
    // rewind 一定要写，让文件指针指向开头
    rewind(fp);
    fread(ps, sizeof(struct seat), MAX, fp);
}

void write_file(struct seat * ps)
{
    FILE * fp;

    fp = fopen("seat.txt", "w+b");
    fwrite(ps, sizeof(struct seat), MAX, fp);
    fclose(fp);
}

char get_choice(void)
{
    char ch;

    printf("To choose a function, enter its letter label:\n");
    printf("a) Show number of empty seats\n");
    printf("b) Show list of empty seats\n");
    printf("c) Show alphabetical list of seats\n");
    printf("d) Assign a customer to a seat assignment\n");
    printf("e) Delete a seat assignment\n");
    printf("f) Quit\n");

    while ((ch = getchar()) < 'a' || ch > 'f')
    {
        while (getchar() != '\n') continue;
        printf("Please input the right letter.\n");
    }
    while (getchar() != '\n') continue;

    return ch;
}

void show_number(struct seat * ps)
{
    int i, count = 0;

    for (i = 0; i < MAX; i++)
        if (ps[i].is_assign == 0)
            count++;
    printf("The number of empty seats is %d\n", count);
}

void show_empty(struct seat * ps)
{
    int i;

    printf("The list of empty seats:\n");
    for (i = 0; i < MAX; i++)
    {
        if (!ps[i].is_assign)
            printf("%d ", ps[i].num);
    }
    printf("\n");
}

void show_list(struct seat * ps)
{
    int i;

    printf("The list of seats:\n");
    for (i = 0; i < MAX; i++)
        printf("%d %s %s\n", ps[i].num, ps[i].f_name, ps[i].l_name);
}

void assign_seat(struct seat * ps)
{
    int number, ret_val;

    printf("Please input the number of seat you want to book (1 to 12):\n");
    while ((ret_val = scanf("%d", &number)) != 1 || number < 1 || number > 12 || ps[number - 1].is_assign == 1)
    {
        if (ret_val != 1 || number < 1 || number > 12)
            printf("Sorry, please input a integer from 1 to 12.\n");
        else
            printf("Sorry, the seat is assigned, please reselect:\n");
        while (getchar() != '\n') continue;
    }
    while (getchar() != '\n') continue;
    printf("Now, input your first name:\n");
    s_gets(ps[number-1].f_name, LEN);
    printf("Input your last name:\n");
    s_gets(ps[number-1].l_name, LEN);
    ps[number - 1].is_assign = 1;
    printf("OK.\n");
}

void delete_seat(struct seat * ps)
{
    int number;
    printf("Please input the number of seat you want to delete (1 to 12):\n");
    while (scanf("%d", &number) != 1 || number < 1 || number > 12 || ps[number - 1].is_assign == 0)
    {
        if (ps[number - 1].is_assign == 0)
            printf("Sorry, the seat is empty, please reselect:\n");
        else
            printf("Sorry, please input a inter from 1 to 12.\n");
        while (getchar() != '\n') continue;
    }
    while (getchar() != '\n') continue;
    strcpy(ps[number - 1].f_name, "");
    strcpy(ps[number - 1].l_name, "");
    ps[number - 1].is_assign = 0;
    printf("Ok.\n");
}

char * s_gets(char * st, int n)
{
    char * ret_val;
    char * find;

    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        find = strchr(st, '\n');
        if (find)
            *find = '\0';
        else
            while (getchar() != '\n') continue;
    }

    return ret_val;
}